package com.commerce.img.activity;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.commerce.img.MyAppData;
import com.commerce.img.R;
import com.commerce.img.adapter.MyFragmentPagerAdapter;
import com.commerce.img.tools.DataFormat;

import java.io.IOException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class WallpaperActivity extends AppCompatActivity implements View.OnClickListener {

    private MyAppData myAppData;
    private TextView textTime1;
    private TextView textTime2;
    private TextView textView;
    private TimerTask timerTask;
    private Timer timer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallpaper);
        myAppData = (MyAppData) getApplication();
        ViewPager viewPager = findViewById(R.id.viewPager);
        textView = findViewById(R.id.wp_btn_wall);
        ImageView back = findViewById(R.id.wp_back);
        textTime1 = findViewById(R.id.wp_time1);
        textTime2 = findViewById(R.id.wp_time2);
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),myAppData.getListClone(),myAppData));
        viewPager.setCurrentItem(myAppData.getI());
        viewPager.setOffscreenPageLimit(1);
        back.setOnClickListener(this);
        textView.setOnClickListener(this);
        timer=new Timer();
        timerTask=new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textTime1.setText(new DataFormat().time1(new Date()));
                        textTime2.setText(new DataFormat().time2(new Date()));
                    }
                });
            }
        };
        timer.schedule(timerTask,0,3000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.wp_back :
                finish();
                break;
            case R.id.wp_btn_wall :
                showPopup(textView);
                break;
        }
    }
    
    public void showPopup(View view) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.getMenuInflater().inflate(R.menu.meun_arlt, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                final WallpaperManager wallpaperManager = WallpaperManager.getInstance(WallpaperActivity.this);
                ImageView iv = myAppData.getImageView();
                iv.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(iv.getDrawingCache());
                iv.setDrawingCacheEnabled(false);
                try {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        switch (item.getItemId()){
                            case R.id.zm :
                                wallpaperManager.setBitmap(bitmap, null, false, WallpaperManager.FLAG_SYSTEM);
                                break;
                            case R.id.sp :
                                wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK);
                                break;
                            case R.id.zmsp:
                                wallpaperManager.setBitmap(bitmap,null,false);
                                break; }
                    }else {
                        wallpaperManager.setBitmap(bitmap);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return true;
            }});
        popupMenu.show();
    }
}